package com.awakening.sorting.basic;

import com.awakening.sorting.util.JavaLikeSwap;

/**
 * 选择排序法——Selection Sort
 * 最为简单的插入排序
 * @Author:AwakeningCode
 * @Date: Created in 23:36 2018\1\16 0016
 */
public class SelectionSort {

    //算法类不允许产生任何实例
    private SelectionSort(){}

    public static void main(String[] args) {
        int[] arr = {10,9,8,7,6,5,4,3,2,1};
        SelectionSort.Sort(arr);
        for (int i = 0 ; i < arr.length ; i ++ ){
            System.out.print(arr[i]);
            System.out.print(' ');
        }
        System.out.println();
    }

    public static void Sort(int arr[]){
        int n = arr.length;
        for (int i = 0 ; i < n ; i ++ ){
            //寻找[i , n)区间里的最小值
            int minIndex = i;
            for (int j = i + 1 ; j < n ; j++){
                if (arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            JavaLikeSwap.swap(arr , i , minIndex);
        }
    }

}
